<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="账户名称" prop="accountName">
        <el-select v-model="queryParams.accountName" clearable placeholder="请选择" @keyup.enter.native="handleQuery">
          <el-option v-for="dict in dict.type.cld_account_name" :key="dict.value" :label="dict.label"
                     :value="parseInt(dict.value)"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="公司名称" prop="companyName">
        <el-input v-model="queryParams.companyName" placeholder="请输入公司名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="银行卡号" prop="bankCardNo">
        <el-input v-model="queryParams.bankCardNo" placeholder="请输入银行卡号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="开户行" prop="openName">
        <el-input v-model="queryParams.openName" placeholder="请输入开户行" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['cld:accountInfo:add']">新增账户</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="accountInfoList" border>
      <el-table-column label="账户名称" align="center" prop="accountName" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_account_name" :value="scope.row.accountName"/>
        </template>
      </el-table-column>
      <el-table-column label="公司名称" align="center" prop="companyName" />
      <el-table-column label="所属人" align="center" prop="belongName" />
      <el-table-column label="银行卡号" align="center" prop="bankCardNo" >
        <template slot-scope="scope">
          {{ scope.row.bankCardNo ? scope.row.bankCardNo.replace(/\s/g, '').replace(/[^\d]/g, '').replace(/(\d{4})(?=\d)/g, '$1 ') : '' }}
        </template>
      </el-table-column>
      <el-table-column label="开户行" align="center" prop="openName" width="300px;"/>
      <el-table-column label="总余额" align="center" prop="amount" >
        <template slot-scope="scope">
          {{ toMoney(scope.row.amount) }}
        </template>
      </el-table-column>
      <el-table-column label="创建人名称" align="center" prop="createName" />
      <el-table-column label="创建时间" align="center" prop="createTime" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['cld:accountInfo:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-finished" @click="handleDetailsView(scope.row)" v-hasPermi="['cld:accountInfo:remove']">流水</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <!-- 添加或修改账户信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="账户名称" prop="accountName">
          <el-select v-model="form.accountName" placeholder="请选择">
            <el-option v-for="dict in dict.type.cld_account_name" :key="dict.value" :label="dict.label"
                       :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="公司名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入公司名称" />
        </el-form-item>
        <el-form-item label="所属人" prop="belongName">
          <el-input v-model="form.belongName" placeholder="请输入所属人" />
        </el-form-item>
        <el-form-item label="银行卡号" prop="bankCardNo">
          <el-input v-model="form.bankCardNo" placeholder="请输入银行卡号" />
        </el-form-item>
        <el-form-item label="开户行" prop="openName">
          <el-input v-model="form.openName" placeholder="请输入开户行" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAccountInfo, getAccountInfo, delAccountInfo, addAccountInfo, updateAccountInfo } from "@/api/cld/accountInfo";

export default {
  name: "account",
  dicts: ['cld_account_name',],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 账户信息表格数据
      accountInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        accountName: null,
        bankCardNo: null,
        companyName: null,
        openName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        accountName: [
          { required: true, message: "请选择账户", trigger: "blur" },
        ],
        companyName: [
          { required: true, message: "请输入公司名称", trigger: "blur" },
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询账户信息列表 */
    getList() {
      this.loading = true;
      listAccountInfo(this.queryParams).then(response => {
        this.accountInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        accountName: null,
        bankCardNo: null,
        companyName: null,
        openName: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加账户信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const accountInfoId = row.accountInfoId || this.ids
      getAccountInfo(accountInfoId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改账户信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.accountInfoId != null) {
            updateAccountInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAccountInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 查看账户流水操作 */
    handleDetailsView(row) {
      this.$router.push("/accountInfo/details/index/view/" + row.accountInfoId);
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('cld/accountInfo/export', {
        ...this.queryParams
      }, `card_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
  .app-container{
    padding:0px;
  }
</style>
